    <?php foreach ($entity as $e): ?>
        <div class="ibox float-e-margins" id="main_windowe">
            <div class="ibox-title" style="border: none;">
                <h5 style="color: #00afef"><i>View</i></h5>
                <div id="form-add-btn" class="ibox-tools">
                    <i id="loading-icon" style="color: gray; display: none;" class="fa fa-spinner fa-spin fa-lg"></i>&nbsp;
                    <a class="btn btn-sm btn-primary save-btn-tab" target="_blank" href="http://wums.bitnamiapp.com/billsmo/api/create_account?entity_id=<?php echo base64_encode($e->entity_id); ?>&company_id=<?php echo base64_encode($this->session->userdata('company_id')); ?>">Create Online Account</a>
                    <button class="btn btn-sm btn-primary save-btn-tab" id="save_edit_entity">Save</button>
                    <button id="cancel_edit" class="btn btn-sm btn-primary btn-outline close-edit cancel-btn-tab">Close</button>
                </div>
            </div>
            <div class="ibox-content">
                <form class="form-horizontal" id="entity_form_e">
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Entity Type</label>
                        <div class="col-lg-5">
                            <input type="hidden" name="entity_id" value="<?php echo $e->entity_id; ?>">
                            <input type="hidden" id="e_type_e" value="<?php echo $e->entity_type; ?>">
                            <select class="form-control m-b choosen" name="entity_type">
                            <?php foreach ($entty_type as $key): ?>
                                <option value="<?php echo $key->code; ?>" <?php echo ($key->code === $e->entity_type) ? 'selected' : ''; ?>><?php echo $key->meaning; ?></option>
                            <?php endforeach;?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group organization">
                        <label class="col-lg-4 control-label">Entity Name</label>
                        <div class="col-lg-5">
                            <input  name="entity_name" type="text" value="<?php echo $e->entity_name; ?>" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Tax Registration Number</label>
                        <div class="col-lg-5">
                            <input name="tax_reg_num" value="<?php echo $e->tax_reg_num; ?>" type="text" class="form-control">
                        </div>
                    </div>
                    <div class="form-group person">
                        <label class="col-lg-4 control-label">Title</label>
                        <div class="col-lg-5">
                            <input name="title" type="text" value="<?php echo $e->title; ?>" class="form-control">
                        </div>
                    </div>
                    <div class="form-group person">
                        <label class="col-lg-4 control-label">Last Name</label>
                        <div class="col-lg-5">
                            <input name="last_name"  type="text" value="<?php echo $e->last_name; ?>" class="form-control">
                        </div>
                    </div>
                    <div class="form-group person">
                        <label class="col-lg-4 control-label">Middle Name</label>
                        <div class="col-lg-5">
                            <input name="middle_name"  type="text" value="<?php echo $e->middle_name; ?>" class="form-control">
                        </div>
                    </div>
                    <div class="form-group person">
                        <label class="col-lg-4 control-label">First Name</label>
                        <div class="col-lg-5">
                            <input name="first_name" type="text" value="<?php echo $e->first_name; ?>" class="form-control">
                        </div>
                    </div>
                    <div class="form-group person" id="data_3">
                        <label class="col-lg-4 control-label">Birthday</label>
                        <div class="col-lg-5">
                            <input name="dob" value="<?php echo $e->date_of_birth; ?>" type="date" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Enable</label>
                        <div class="col-lg-1">
                            <input <?php echo $e->enable_flag == 'Y' ? 'checked' : ''; ?> type="checkbox" class="i-checks" value="ok" name="ena_flag" >
                        </div>
                    </div>
                    </form>
                </div>
            <?php endforeach;?>
                <div class="ibox-title" style="border: none;">
                    <h5><b>Main Address</b></h5>
                </div>
                <div class="ibox-content">
                <?php if (empty($addr)) {echo "<h5>Empty Address<h5>";}
?>
                <?php foreach ($addr as $a): ?>
                    <form class="form-horizontal" id="entity_address_form_e">
                        <input type="hidden" name="entity_id" value="<?php echo $a->entity_id; ?>">
                        <input type="hidden" name="address_id" value="<?php echo $a->address_id; ?>">
                        <input type="hidden" name="addr_code" value="MAIN">
                        <div class="form-group">
                            <label class="col-lg-4 control-label">Address 1</label>
                            <div class="col-lg-5">
                                <input name="addr_1" value="<?php echo $a->address_1; ?>" type="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-4 control-label">Address 2</label>
                            <div class="col-lg-5">
                                <input name="addr_2"  value="<?php echo $a->address_2; ?>" type="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-4 control-label">Address 3</label>
                            <div class="col-lg-5">
                                <input name="addr_3" value="<?php echo $a->address_3; ?>" type="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-4 control-label">City / Town</label>
                            <div class="col-lg-5">
                                <input name="city_town" value="<?php echo $a->city_town; ?>" type="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-4 control-label">Postal Code</label>
                            <div class="col-lg-5">
                                <input name="postal_code" value="<?php echo $a->postal_code; ?>" type="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-4 control-label">Province / State</label>
                            <div class="col-lg-5">
                                <input name="state_province" value="<?php echo $a->state_province; ?>" type="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-4 control-label">Country</label>
                            <input type="hidden" name="county_tmp" value="<?php echo $a->country; ?>">
                            <div class="col-lg-5">
                                <select class="form-control m-b choosen" id="countries" name="country">
                                <option value="">Country . .</option>
                                <?php foreach ($country as $key): ?>
                                    <option value="<?php echo $key->country_code; ?>" <?php echo $key->country_code == $a->country ? 'selected' : ''; ?>><?php echo $key->country_name; ?></option>
                                <?php endforeach;?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-4 control-label">Phone 1</label>
                            <div class="col-lg-5">
                                <input name="phone_1" value="<?php echo $a->phone_1; ?>" type="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-4 control-label">Phone 2</label>
                            <div class="col-lg-5">
                                <input name="phone_2" value="<?php echo $a->phone_2; ?>" type="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-4 control-label">Phone 3</label>
                            <div class="col-lg-5">
                                <input name="phone_3" value="<?php echo $a->phone_3; ?>" type="text" class="form-control">
                            </div>
                        </div>
                        <input type="hidden" name="a_zone_1" value="<?php echo $a->zone_1; ?>">
                        <input type="hidden" name="a_zone_2" value="<?php echo $a->zone_2; ?>">
                        <input type="hidden" name="a_zone_3" value="<?php echo $a->zone_3; ?>">
                        <input type="hidden" name="a_zone_4" value="<?php echo $a->zone_4; ?>">
                        <input type="hidden" name="a_zone_5" value="<?php echo $a->zone_5; ?>">
                        <?php foreach ($zone1 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                            <div class="col-lg-5">
                                <select name="zone_1" class="form-control chosen-select">
                                    <option value=""></option>
                                <?php foreach ($zone as $key): ?>
                                    <option <?php echo ($key->zone_value_name == $a->zone_1 ? 'selected' : ''); ?> value="<?php echo $key->zone_value_name; ?>"><?php echo $key->zone_value_name; ?></option>
                                <?php endforeach;?>
                                </select>
                            </div>
                        </div>
                         <?php foreach ($zone2 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                            <div class="col-lg-5">
                                <select name="zone_2" class="form-control chosen-select">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>
                         <?php foreach ($zone3 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                            <div class="col-lg-5">
                                <select name="zone_3" class="form-control chosen-select">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>
                         <?php foreach ($zone4 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                            <div class="col-lg-5">
                               <select name="zone_4" class="form-control chosen-select">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>
                          <?php foreach ($zone5 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                            <div class="col-lg-5">
                                <select name="zone_5" class="form-control chosen-select">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>
                        <input type="hidden" name="purpose" value="">
                        <input type="hidden" name="ena_flag_a" value="Y">
                    </form>
                </div>
                <div class="ibox-title" style="border: none;">
                    <div class="col-lg-2">
                        <button id="add_addr_btn_e" type="button" class="btn btn-w-m btn-info add-tab-btn">Add Address</button>
                    </div>
                </div>
                <div class="ibox-content">
                </div>
                <div class="ibox-title" style="border: none;">
                    <h5><i>List of Other Address's</i></h5>
                </div>
                <table class="table">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Address Code</th>
                            <th>Address 1</th>
                            <th>Enable</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody id="table_address">
                    <?php $count2 = 1;?>
                    <?php foreach ($addr2 as $a2): ?>
                        <tr <?php echo $a2->addr_code == 'MAIN' ? 'style="display: none;"' : ''; ?> <?php echo $a2->addr_code == 'MAIN' ? $count2 = $count2 - 1 : ''; ?>>
                            <td><?php echo $count2; ?></td>
                            <td><?php echo $a2->addr_code; ?></td>
                            <td><?php echo $a2->address_1; ?></td>
                            <td align="center"><div class="state icheckbox_square-green <?php echo $a2->enable_flag == 'Y' ? 'checked' : ''; ?>"></div></td>
                            <td style="display: none;" id="addr_id_e"><?php echo $a2->address_id; ?></td>
                            <td><button class='btn btn-sm btn-outline btn-info btn-xs edit-modal'><i class='fa fa-pencil-square-o fa-lg'></i></button></td>
                        </tr>
                    <?php $count2++;?>
                    <?php endforeach;?>
                    </tbody>
                </table>
                <div class="ibox-title" style="border: none;">
                    <div class="col-lg-2">
                        <button id="add_acc_btn_edit" class="btn btn-w-m btn-info add-tab-btn">Add Account</button>
                    </div>
                </div>
                <div class="ibox-content">
                </div>
                <div class="ibox-title" style="border: none;">
                    <h5><i>List of Other Account's</i></h5>
                </div>
                <table class="table">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Account Name</th>
                            <th>Account Type</th>
                            <th>Enable</th>
                            <th>Status</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $count = 1;?>
                        <?php foreach ($acct2 as $ac):
	$input;
	switch ($ac->status) {
	case "INACT":
		$color = "danger";
		break;
	case "ACT":
		$color = "primary";
		break;
	case "PND":
		$color = "warning";
		break;
	default:
		$color = "default";
	}
	?>
      <tr>
      <td><?php echo $count; ?></td>
      <td><?php echo $ac->acct_name; ?></td>
      <td><?php echo $ac->meaning; ?></td>
      <td align="center"><div class="state icheckbox_square-green <?php echo $ac->enable_flag == 'Y' ? 'checked' : ''; ?>"></div></td>
      <td><p><span class='label label-<?php echo $color; ?>'><?php echo $ac->status_meaning; ?></span></p></td>
      <td style="display: none;" id="acct_addr"><?php echo $ac->acct_addr; ?></td>
      <td style="display: none;" id="acct_id"><?php echo $ac->account_id; ?></td>
      <td><button class='btn btn-sm btn-outline btn-info btn-xs edit-modal-acc' style="display:none;"><i class='fa fa-pencil-square-o fa-lg'></i></button></td>
      </tr>
      <?php $count++;?>
      <?php endforeach;?>
                    </tbody>
                </table>
            </div>
        </div>
    </div><!-- iBox -->
    <?php endforeach;?>
    <div class="ibox float-e-margins" id="address_windowe" style="display: none;">
        <div class="ibox-title" style="border: none;">
            <h5 style="color: #00afef"><i>Address</i></h5>
            <div class="ibox-tools">
                <button style="display:none;" id="save_add_address_btn" type="button" class="btn btn-primary btn-sm save-btn-tab">Save</button>
                <button id="save_edit_address_btn" type="button" class="btn btn-primary btn-sm save-btn-tab">Save</button>
                <button type="button" class="btn btn-white btn-sm cancel-btn-tab" id="address_close_btn">Close</button>
                <!-- <a href="#chglist_form_e" class="btn btn-info btn-outline btn-sm save-btn-tab" id="click_list_edit">Go to Chargelist</button> -->
            </div>
        </div>
        <div class="ibox-content" id="addr_modal_body">
            <form class="form-horizontal" id="entity_address_form_2_e">
                <input type="hidden" name="entity_id">
                <input type="hidden" name="address_id">
                <div class="form-group">
                    <label class="col-lg-4 control-label">Address Code</label>
                    <div class="col-lg-6">
                        <input name="addr_code" id="addr_code"  type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Address 1</label>
                    <div class="col-lg-6">
                        <input name="addr_1" id="addr_1" type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Address 2</label>
                    <div class="col-lg-6">
                        <input name="addr_2" id="addr_2" type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Address 3</label>
                    <div class="col-lg-6">
                        <input name="addr_3" id="addr_3" type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">City / Town</label>
                    <div class="col-lg-6">
                        <input name="city_town" id="citytown" type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Postal Code</label>
                    <div class="col-lg-6">
                        <input name="postal_code" id="postalcode" type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Province / State</label>
                    <div class="col-lg-6">
                        <input name="state_province" id="provincestate" type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Country</label>
                    <div class="col-lg-6">
                        <!-- <input name="country" id="country_v" type="text" class="form-control"> -->
                        <select id="country_v" class="form-control m-b choosen" name="country">
                            <option value="">Country . . .</option>
                            <?php foreach ($country as $key): ?>
                            <option value="<?php echo $key->country_code; ?>"><?php echo $key->country_name; ?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Phone 1</label>
                    <div class="col-lg-6">
                        <input name="phone_1" id="phone1" type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Phone 2</label>
                    <div class="col-lg-6">
                        <input name="phone_2" id="phone2" type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Phone 3</label>
                    <div class="col-lg-6">
                        <input name="phone_3" id="phone3" type="text" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Purpose</label>
                    <div class="col-lg-6">
                        <!-- <input name="purpose" id="purpose_v" type="text" class="form-control"> -->
                         <select class="form-control m-b choosen" id="purpose_v" name="purpose">
                            <option value="">Select Purpose</option>
                            <?php foreach ($addr_pur as $key): ?>
                            <option value="<?php echo $key->purpose; ?>"><?php echo $key->meaning; ?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Enable</label>
                    <div class="col-lg-1">
                        <input name="ena_flag_a" type="checkbox" checked id="enable_flag_a" class="i-checks">
                    </div>
                </div>
                <h4 id="overview" class="page-header">Address Zone</h4>
               <?php foreach ($zone1 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                            <div class="col-lg-7">
                                <select name="zone_1" class="form-control chosen-select" id="zn_1">
                                    <option value=""></option>
                                <?php foreach ($zone as $key): ?>
                                    <option <?php echo ($key->zone_value_name == $a->zone_1 ? 'selected' : ''); ?> value="<?php echo $key->zone_value_name; ?>"><?php echo $key->zone_value_name; ?></option>
                                <?php endforeach;?>
                                </select>
                            </div>
                        </div>
                <?php foreach ($zone2 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                    <div class="col-lg-7">
                        <select id="zn_2" name="zone_2" class="form-control chosen-select">
                                    <option value=""></option>
                                </select>
                    </div>
                </div>
               <?php foreach ($zone3 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                    <div class="col-lg-7">
                        <select id="zn_3" name="zone_3" class="form-control chosen-select">
                                    <option value=""></option>
                                </select>
                    </div>
                </div>
                <?php foreach ($zone4 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                    <div class="col-lg-7">
                        <select id="zn_4" name="zone_4" class="form-control chosen-select">
                                    <option value=""></option>
                                </select>
                    </div>
                </div>
                <?php foreach ($zone5 as $key): ?>
                        <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                                <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                            <?php endforeach;?>
                    <div class="col-lg-7">
                        <select id="zn_5" name="zone_5" class="form-control chosen-select">
                                    <option value=""></option>
                                </select>
                    </div>
                </div>
            </form>
        </div>
    </div><!-- END OF MODAL-->
    <div class="ibox float-e-margins" id="account_windowe" style="display: none;">
        <div class="ibox-title" style="border: none;">
            <h5 style="color: #00afef"><i>Account</i></h5>
            <div class="ibox-tools">
                <button style="display: none;" type="button" id="save_add_acc_btn" class="btn btn-primary btn-sm save-btn-tab">Save</button>
                <button type="button" id="save_edit_acc_btn" class="btn btn-primary btn-sm save-btn-tab">Save</button>
                <button type="button" class="btn btn-white btn-sm cancel-btn-tab" id="account_close_btn">Close</button>
            </div>
        </div>
            <div class="ibox-content" id="acc_modal_body">
                <form class="form-horizontal" id="account_form_edit">
                <input type="hidden" name="account_id">
                <input type="hidden" name="entity_id">
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Entity Name</label>
                        <div class="col-lg-6">
                            <input readonly type="text" name="entity_name" id="entityname" class="form-control">
                        </div>
                    </div>
                   <div class="form-group">
                        <label class="col-lg-4 control-label">Account Number</label>
                        <div class="">
                            <div class="input-group">
                                <div id="qnoeditable">
                                </div>
                                <div id="qyeseditable">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Account Name</label>
                        <div class="col-lg-6">
                            <input name="acct_name" type="text" id="acctname" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Account Type</label>
                        <div class="col-lg-6">
                            <select class="form-control m-b" name="acct_type" id="accttype">
                                <option value="">Select Type</option>
                                <?php foreach ($acct_type as $key): ?>
                                <option value="<?php echo $key->code; ?>"><?php echo $key->meaning; ?></option>
                                <?php endforeach;?>
                            </select>
                        </div>
                    </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Meter Size</label>
                    <div class="col-lg-6">
                        <select class="form-control m-b chosen-select" name="meter_size" id="mtrsize">
                            <option value="">Select Type</option>
                            <?php foreach ($meter_size as $key): ?>
                            <option value="<?php echo $key->lookup_code; ?>"><?php echo $key->meaning; ?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Attention to</label>
                        <div class="col-lg-6">
                            <input  name="attention_to" type="text" id="attentionto" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Account Address</label>
                        <div class="col-lg-6">
                            <select class="form-control m-b" id="acctaddr" name="acct_addr">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Bill To Address</label>
                        <div class="col-lg-6">
                            <select id="billtoaddr" class="form-control m-b" name="bill_to_addr">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Active Date From</label>
                        <div class="col-lg-6">
                            <input name="active_start"  type="date" id="activestart" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Active Date To</label>
                        <div class="col-lg-6">
                            <input name="active_end" type="date" id="activeend" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Meter Sequence</label>
                        <div class="col-lg-6">
                            <input type="input" name="meter_seq" value="" id="meterseq" class="form-control text-right">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Location Latitude</label>
                        <div class="col-lg-6">
                            <input type="text" name="ent_lat" class="form-control" id="lat_ea" value="0" readonly >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Location Longitude</label>
                        <div class="col-lg-6">
                            <input type="text" name="ent_lng" class="form-control" id="lng_ea" value="0" readonly >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Pick Location</label>
                        <div class="col-lg-6">
                            <div id="map_wrapper" style="height: 300px;">
                                <div id="map_canvas" class="mapping" style="width: 100%;height: 100%;"></div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Status</label>
                        <div class="col-lg-6">
                            <input readonly type="text" id="status_v2" value=""  class="form-control">
                            <input readonly type="hidden" name="status" value="PND" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">Enable</label>
                        <div class="col-lg-1">
                            <input type="checkbox" id="ena_flag_v" class="i-checks" value="ok" name="ena_flag" >
                        </div>
                    </div>
                </form>
                <form class="form-horizontal" id="zone_form">
                    <h4 id="overview" class="page-header">Address Zone <small></small></h4>
                    <?php foreach ($zone1 as $key): ?>
                <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                        <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                    <?php endforeach;?>
                        <div class="col-lg-7">
                            <input readonly type="text" id="z1" class="form-control">
                        </div>
                    </div>
                     <?php foreach ($zone2 as $key): ?>
                <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                        <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                    <?php endforeach;?>
                        <div class="col-lg-7">
                            <input readonly type="text" id="z2"  class="form-control">
                        </div>
                    </div>
                     <?php foreach ($zone3 as $key): ?>
                <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                        <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                    <?php endforeach;?>
                        <div class="col-lg-7">
                            <input readonly type="text" id="z3"  class="form-control">
                        </div>
                    </div>
                    <?php foreach ($zone4 as $key): ?>
                <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                        <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                    <?php endforeach;?>
                        <div class="col-lg-7">
                            <input readonly type="text" id="z4"  class="form-control">
                        </div>
                    </div>
                    <?php foreach ($zone5 as $key): ?>
                <div class="form-group" style="display: <?php echo ($key->enable_flag == 'N' ? 'none' : ''); ?>">
                        <label class="col-lg-4 control-label"><?php echo $key->field_label; ?></label>
                    <?php endforeach;?>
                        <div class="col-lg-7">
                            <input readonly type="text" id="z5" class="form-control">
                        </div>
                    </div>
                </form>
            <h4 class="page-header">Charge List</h4>
                <div class="">
                <form id="chglist_form_e" class="form-horizontal">
                    <table class="table table-bordered" width="100%">
                        <thead>
                            <tr>
                                <th>Charge List</th>
                                <th>Start Date</th>
                                <th>End Date</th>
                                <th>Enable</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tr id="chglistform">
                            <td style="margin:0px;padding:0px;">
                                <select class="form-control-custom chg-list" name="list_id">
                                    <option value="">--</option>
                                    <?php foreach ($chrglist as $key): ?>
                                    <option value="<?php echo $key->list_id; ?>"><?php echo $key->list_name; ?></option>
                                    <?php endforeach;?>
                                </select>
                            </td>
                            <input type="hidden" name="acct_chrg_id">
                            <td style="margin:0px;padding:0px;vertical-align: middle;"><input style="width: 100%;" class="form-control-custom" type="date" name="s_date" value="<?php echo date('Y-m-d'); ?>"></td>
                            <td style="margin:0px;padding:0px;vertical-align: middle;"><input style="width: 100%;" class="form-control-custom" type="date" name="e_date" value="2080-01-01"></td>
                            <td style="margin:0px;padding:0px;vertical-align: middle;" align="center"><input style="width: 100%;" type="checkbox" name="enable_flag" class="i-checks"></td>
                            <td style="margin:0px;padding:0px;vertical-align: middle;" align="center"><button style="width: 100%;margin-bottom:0px;" id="chglist_save_btn" type="button" class="btn btn-primary btn-outline btn-block"><i class="fa fa-check fa-fw"></i></button><button style="width: 50%;display:none;margin-bottom:0px;" id="save_edit_chglist" type="button" class="btn btn-primary btn-outline chglist_xtra_btn"><i class="fa fa-check fa-fw"></i></button><button style="width: 50%;display:none;margin-bottom:0px;" id="undo_chglist" type="button" class="btn btn-danger btn-outline chglist_xtra_btn"><i class="fa fa-undo fa-fw"></i></button></td>
                        </tr>
                        <tbody id="charglist_tbl">
                        </tbody>
                    </table>
                    </div>
                </form>
            <h4 id="overview" class="page-header">&nbsp;<button id="add_dev_btn_edit" type="button" class="btn btn-w-m btn-info add-tab-btn">Add Device</button></h4>
            <div class="ibox-title" style="border: none;">
                <h5><i>List of Other Device's</i></h5>
            </div>
            <table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Device ID</th>
                        <th>Model Nubmer</th>
                        <th>Serial Number</th>
                        <th>Status</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody id="device_table_edit">
                </tbody>
            </table>
        </div>
    </div><!-- END OF MODAL-->
    <div class="ibox float-e-margins" id="device_windowe" style='display:none;'>
        <div class="ibox-title" style="border: none;">
            <h5 style="color: #00afef"><i>Devices</i></h5>
            <div class="ibox-tools">
                <button style="display: none;" type="button" id="ins_dev_btn_edit" class="btn btn-primary btn-sm save-btn-tab">Save</button>
                <button type="button" id="upd_dev_btn" class="btn btn-primary btn-sm save-btn-tab">Save</button>
                <button type="button" class="btn btn-white btn-sm cancel-btn-tab" id="device_close_btn">Close</button>
            </div>
        </div>
        <div class="ibox-content" id="dev_modal_body">
            <div style="display: none;" id="loading_dev_edit" class="spiner-example">
                <div class="sk-spinner sk-spinner-rotating-plane"></div>
            </div>
            <form class="form-horizontal" id="device_form_e">
                <input type="hidden" name="account_id">
                <input type="hidden" name="device_ass_id">
                <div class="form-group">
                    <label class="col-lg-4 control-label">Item Number</label>
                    <div class="col-lg-7">
                        <select class="form-control m-b" name="item_num">
                            <option value="">Select Status</option>
                            <?php foreach ($item as $i): ?>
                            <option value="<?php echo $i->item_id; ?>"><?php echo $i->item_num; ?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Device Type</label>
                    <div class="col-lg-7">
                        <select class="form-control m-b" name="dev_type">
                            <option value="">Select Status</option>
                            <?php foreach ($devtype as $dt): ?>
                            <option value="<?php echo $dt->type; ?>"><?php echo $dt->meaning; ?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </div>
                 <div class="form-group">
                    <label class="col-lg-4 control-label">Model Number</label>
                    <div class="col-lg-7">
                        <input type="text" name="model_number" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Serial Number</label>
                    <div class="col-lg-7">
                        <input type="text" name="serial_number" class="form-control">
                    </div>
                </div>
                 <div class="form-group">
                    <label class="col-lg-4 control-label">Device ID</label>
                    <div class="col-lg-7">
                        <input   type="text" name="dev_id" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                        <label class="col-lg-4 control-label">Location Latitude</label>
                    <div class="col-lg-6">
                        <input type="text" name="e_lat_d" id="eLat_dv" class="form-control" value="0" readonly>
                    </div>
                </div>
                <div class="form-group">
                        <label class="col-lg-4 control-label">Location Longitude</label>
                    <div class="col-lg-6">
                        <input type="text" name="e_lng_d" id="eLng_dv" class="form-control" value="0" readonly>
                    </div>
                </div>
                <div class="form-group">
                        <label class="col-lg-4 control-label">Pick Location</label>
                    <div class="col-lg-6">
                        <div id="map_wrapper" style="height: 300px;">
                            <div id="map_canvas2" class="mapping" style="width: 100%;height: 100%;">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Installation Date</label>
                    <div class="col-lg-7">
                        <input type="text" name="install_date" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Warranty Period</label>
                    <div class="col-lg-7">
                        <input type="text" name="warranty_per" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Initial Reading</label>
                    <div class="col-lg-7">
                        <input type="text" name="initial_read" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Last Reading</label>
                    <div class="col-lg-7">
                        <input type="text" name="last_read" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Status</label>
                    <div class="col-lg-7">
                        <select class="form-control m-b" name="status">
                            <option value="">Select Status</option>
                            <?php foreach ($devstats as $ds): ?>
                            <option value="<?php echo $ds->status; ?>"><?php echo $ds->meaning; ?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-4 control-label">Device Remarks</label>
                    <div class="col-lg-7">
                        <input type="text" name="dev_remarks" class="form-control">
                    </div>
                </div>
            </form>
        </div>
    </div><!-- END OF MODAL-->


    <script type="text/javascript">
            $(document).ready(function() {
                $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green hovered',
                    radioClass: 'iradio_square-green'
                });

                getflexseqs2_e();
                window.setInterval(function(){
                  getflexseqse();
                }, 1500);
                function getflexseqse(){
                    $.ajax({
                        url: "<?php echo base_url('sys/flex_seqs/get_flex_seqs'); ?>",
                        method:'POST',
                        dataType:'json',
                        data: $('#account_form_edit').serialize()+'&'+$.param({'table_name' : 'ar_account', 'table_field' : 'acct_num'}),
                        success: function (data) {
                            var tmp_y = '';
                            var tmp_n = '';
                            $.each(data,function(i,item) {
                                //console.log(data[i].editable_flag);
                                if(data[i].editable_flag == 'Y'){
                                    //$("#"+data[i].seg_name+"_fc").val(data[i].value);
                                }else{ 
                                    $("#"+data[i].seg_name+"_fc").val(data[i].value);
                                }
                            }); 
                        }
                    });
                }

                function getflexseqs2_e(){
                    $.ajax({
                        url: "<?php echo base_url('sys/flex_seqs/get_flex_seqs_2'); ?>",
                        method:'POST',
                        dataType:'json',
                        data: $('#account_form_edit').serialize()+'&'+$.param({'table_name' : 'ar_account', 'table_field' : 'acct_num'}),
                        success: function (data) {
                            var tmp_y = '';
                            var tmp_n = '';
                            $.each(data,function(i,item) {
                                //console.log(data[i].editable_flag);
                                if(data[i].editable_flag == 'Y'){
                                    tmp_y += '<div class="col-lg-'+(parseFloat(data[i].seg_length) + 1)+'"><input type="text" maxlength="'+data[i].seg_length+'" id="'+data[i].seg_name+'_fc" value="'+data[i].value+'" class="form-control text-right"/></div>';
                                }else{ 
                                    tmp_n += '<div class="col-lg-'+(parseFloat(data[i].seg_length) + 1)+'"><input type="text" readonly maxlength="'+data[i].seg_length+'" id="'+data[i].seg_name+'_fc" '+(data[i].value != '' ? "value='"+data[i].value+ "' " : '')+' class="form-control text-right"/></div>';
                                }
                            }); 
                            $('#qyeseditable').html(tmp_y);
                            $('#qnoeditable').html(tmp_n);
                        }
                    });
                }

                function concat_flexee(){
                    var concat = '';
                    $.ajax({
                        url: "<?php echo base_url('sys/flex_seqs/get_flex_seqs_2'); ?>",
                        method:'POST',
                        dataType:'json',
                        async: false,
                        data: $('#account_form_edit').serialize()+'&'+$.param({'table_name' : 'ar_account', 'table_field' : 'acct_num'}),
                        success: function (data) {
                            var count = 0;
                            $.each(data,function(i,item) {
                                if(data[i].editable_flag == 'Y'){
                                     concat += $("#"+data[i].seg_name+"_fc").val();
                                }else{ 
                                    concat += data[i].value;
                                }
                                if(count != (data.length - 1)){
                                    concat += data[i].separator;
                                }
                                count++;
                            });
                        }
                    });
                    return concat;
                }

                //-----------------

                $('#add_dev_btn_edit').hide();
                $("select").chosen({width: "100%"});
                $("select").trigger("chosen:updated");
                $('#entity_form_e').data('serialize',$('entity_form_e').serialize());
                sessionStorage.account_edit_changes = 0;
                sessionStorage.address_edit_changes = 0;
                getCode();
                zoning();
                etype_e($('#e_type_e').val());

                $('#account_form_edit #accttype').change(function(){
                    $('#account_form_edit #account_typee').text($(this).val());
                });

                $('#account_form_edit #mtrsize').change(function(){
                    $('#account_form_edit #meter_sizee').text($(this).val());
                });

                function zoning(){
                    var z1 = $('#entity_address_form_e input[name=a_zone_1]').val();
                    var z2 = $('#entity_address_form_e input[name=a_zone_2]').val();
                    var z3 = $('#entity_address_form_e input[name=a_zone_3]').val();
                    var z4 = $('#entity_address_form_e input[name=a_zone_4]').val();
                    var z5 = $('#entity_address_form_e input[name=a_zone_5]').val();

                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': z1 },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                    if(z2 == data[i].zone_value_name){
                                        state = 'selected';
                                    }else{
                                        state = '';
                                    }
                                   tmp += '<option '+state+' value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_e select[name=zone_2]').html( tmp );
                                $("#entity_address_form_e select[name=zone_2]").trigger("chosen:updated");
                        }
                    });

                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': z2 },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                    if(z3 == data[i].zone_value_name){
                                        state = 'selected';
                                    }else{
                                        state = '';
                                    }
                                   tmp += '<option '+state+' value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_e select[name=zone_3]').html( tmp );
                                $("#entity_address_form_e select[name=zone_3]").trigger("chosen:updated");
                        }
                    });

                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': z3 },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                    if(z3 == data[i].zone_value_name){
                                        state = 'selected';
                                    }else{
                                        state = '';
                                    }
                                   tmp += '<option '+state+' value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_e select[name=zone_4]').html( tmp );
                                $("#entity_address_form_e select[name=zone_4]").trigger("chosen:updated");
                        }
                    });

                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': z4 },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                    if(z4 == data[i].zone_value_name){
                                        state = 'selected';
                                    }else{
                                        state = '';
                                    }
                                   tmp += '<option '+state+' value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_e select[name=zone_5]').html( tmp );
                                $("#entity_address_form_e select[name=zone_5]").trigger("chosen:updated");
                        }
                    });
                }

                function getzoness(z1,z2,z3,z4,z5){
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': z1 },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                    if(z2 == data[i].zone_value_name){
                                        state = 'selected';
                                    }else{
                                        state = '';
                                    }
                                   tmp += '<option '+state+' value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_2_e select[name=zone_2]').html( tmp );
                                $("#entity_address_form_2_e select[name=zone_2]").trigger("chosen:updated");
                        }
                    });

                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': z2 },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                    if(z3 == data[i].zone_value_name){
                                        state = 'selected';
                                    }else{
                                        state = '';
                                    }
                                   tmp += '<option '+state+' value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_2_e select[name=zone_3]').html( tmp );
                                $("#entity_address_form_2_e select[name=zone_3]").trigger("chosen:updated");
                        }
                    });

                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': z3 },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                    if(z3 == data[i].zone_value_name){
                                        state = 'selected';
                                    }else{
                                        state = '';
                                    }
                                   tmp += '<option '+state+' value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_2_e select[name=zone_4]').html( tmp );
                                $("#entity_address_form_2_e select[name=zone_4]").trigger("chosen:updated");
                        }
                    });

                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': z4 },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                    if(z4 == data[i].zone_value_name){
                                        state = 'selected';
                                    }else{
                                        state = '';
                                    }
                                   tmp += '<option '+state+' value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_2_e select[name=zone_5]').html( tmp );
                                $("#entity_address_form_2_e select[name=zone_5]").trigger("chosen:updated");
                        }
                    });

                }

                

                $('#entity_address_form_e select[name=zone_1]').change(function(){
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': $(this).val() },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                   tmp += '<option value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_e select[name=zone_2]').html( tmp );
                                $("#entity_address_form_e select[name=zone_2]").trigger("chosen:updated");
                        }
                    });
                });

                $('#entity_address_form_e select[name=zone_2]').change(function(){
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': $(this).val() },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                   tmp += '<option value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_e select[name=zone_3]').html( tmp );
                                $("#entity_address_form_e select[name=zone_3]").trigger("chosen:updated");
                        }
                    });
                });

                $('#entity_address_form_e select[name=zone_3]').change(function(){
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': $(this).val() },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                   tmp += '<option value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_e select[name=zone_4]').html( tmp );
                                $("#entity_address_form_e select[name=zone_4]").trigger("chosen:updated");
                        }
                    });
                });

                $('#entity_address_form_e select[name=zone_4]').change(function(){
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': $(this).val() },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                   tmp += '<option value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_e select[name=zone_5]').html( tmp );
                                $("#entity_address_form_e select[name=zone_5]").trigger("chosen:updated");
                        }
                    });
                });

                $('#entity_address_form_2_e select[name=zone_1]').change(function(){
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': $(this).val() },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                   tmp += '<option value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_2_e select[name=zone_2]').html( tmp );
                                $("#entity_address_form_2_e select[name=zone_2]").trigger("chosen:updated");
                        }
                    });
                });

                $('#entity_address_form_2_e select[name=zone_2]').change(function(){
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': $(this).val() },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                   tmp += '<option value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_2_e select[name=zone_3]').html( tmp );
                                $("#entity_address_form_2_e select[name=zone_3]").trigger("chosen:updated");
                        }
                    });
                });

                $('#entity_address_form_2_e select[name=zone_3]').change(function(){
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': $(this).val() },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                   tmp += '<option value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_2_e select[name=zone_4]').html( tmp );
                                $("#entity_address_form_2_e select[name=zone_4]").trigger("chosen:updated");
                        }
                    });
                });

                $('#entity_address_form_2_e select[name=zone_4]').change(function(){
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/getzone2',
                        method: 'POST',
                        data: { 'parent_id': $(this).val() },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                   tmp += '<option value="'+data[i].zone_value_name+'">'+data[i].zone_value_name+'</option>';
                                });
                                $('#entity_address_form_2_e select[name=zone_5]').html( tmp );
                                $("#entity_address_form_2_e select[name=zone_5]").trigger("chosen:updated");
                        }
                    });
                });

                var markers = [];

                function initialize_dev() {
                   var current=new google.maps.LatLng($('#eLat_dv').val(),$('#eLng_dv').val());

                   var sample = new google.maps.LatLng(8.347557925845486,124.27363961935043);
                   var mapProp = {
                    center:new google.maps.LatLng(8.347557925845486,124.27363961935043),
                    zoom:17,
                    mapTypeId:google.maps.MapTypeId.SATELITE
                  };

                 
                  var map=new google.maps.Map(document.getElementById("map_canvas2"),mapProp);
                    google.maps.event.addDomListener(window, 'load', initialize_dev);

                    if($('#eLat_dv').val() != 0 || $('#eLng_dv').val() != 0){
                       var marker=new google.maps.Marker({
                          position:current,
                          });
                        markers.push(marker);
                        marker.setMap(map);
                        map.panTo(current);
                    }
                  map.addListener('click', function(e) {
                    placeMarkerAndPanTo_dev(e.latLng, map);
                  });  
                // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
                }


                function placeMarkerAndPanTo_dev(latLng, map) {
                  DeleteMarkers();
                  var marker = new google.maps.Marker({
                    position: latLng,
                    map: map
                  });
                  map.panTo(latLng);

                  markers.push(marker);
                  $('#eLat_dv').val(latLng.lat());
                  $('#eLng_dv').val(latLng.lng());

                }

                 function initialize() {
                   var current=new google.maps.LatLng($('#lat_ea').val(),$('#lng_ea').val());
                   var sample = new google.maps.LatLng(8.347557925845486,124.27363961935043);
                   var mapProp = {
                    center:new google.maps.LatLng(8.347557925845486,124.27363961935043),
                    zoom:17,
                    mapTypeId:google.maps.MapTypeId.SATELITE
                  };

                 
                  var map=new google.maps.Map(document.getElementById("map_canvas"),mapProp);
                    google.maps.event.addDomListener(window, 'load', initialize);


                   var marker=new google.maps.Marker({
                      position:current,
                      });
                    markers.push(marker);
                    marker.setMap(map);
                    map.panTo(current);

                  map.addListener('click', function(e) {
                    placeMarkerAndPanTo(e.latLng, map);
                  });  
                // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
                }

                function placeMarkerAndPanTo(latLng, map) {
                  DeleteMarkers();
                  var marker = new google.maps.Marker({
                    position: latLng,
                    map: map
                  });
                  map.panTo(latLng);

                  markers.push(marker);
                  $('#lat_ea').val(latLng.lat());
                  $('#lng_ea').val(latLng.lng());

                }


                function initialize_add() {
                    // lumbia = 8.397440728062685 ,124.59655725862831
                   var mapProp = {
                    center:new google.maps.LatLng(8.347557925845486,124.27363961935043),
                    zoom:17,
                    mapTypeId:google.maps.MapTypeId.SATELITE
                  };
                  var map=new google.maps.Map(document.getElementById("map_canvas"),mapProp);
                    google.maps.event.addDomListener(window, 'load', initialize);

                  map.addListener('click', function(e) {
                    placeMarkerAndPanTo(e.latLng, map);
                  });  
                // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
                }


                function placeMarkerAndPanTo_add(latLng, map) {
                  DeleteMarkers();
                  var marker = new google.maps.Marker({
                    position: latLng,
                    map: map
                  });
                  map.panTo(latLng);

                  markers.push(marker);
                  $('#lat_e').val(latLng.lat());
                  $('#lng_e').val(latLng.lng());

                }



                function DeleteMarkers() {
                //Loop through all the markers and remove
                    for (var i = 0; i < markers.length; i++) {
                        markers[i].setMap(null);
                    }
                    markers = [];
                }

               function getchglist(){
                $('#charglist_tbl').html('');
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/get_chglist',
                        method: 'POST',
                        data: { 'account_id': sessionStorage.account_id_e },
                        dataType: 'json',
                        success: function(data){
                            console.log(data.length);
                            if(data.length !== 0){
                                $('#chglistform').hide();
                                var tmp = '';
                                var state = '';
                                $.each(data, function(i,item){
                                    if(data[i].enable_flag == 'Y'){
                                        state = 'checked';
                                    }
                                    tmp += "<tr id='rmv_chglist'><td style='display:none;' id='acct_chrg_id_v'>"+ data[i].acct_chrglst_id +"</td><td style='display:none;' id='enable_flag_v'>"+ data[i].enable_flag +"</td><td style='display:none;' id='list_id_v'>"+ data[i].list_id +"</td><td id='list_name_v' style='margin:0px;padding:0px;vertical-align: middle;'>"+ data[i].list_name +"</td><td align='right' id='start_date_v'  style='margin:0px;padding:0px;vertical-align: middle;'>"+ data[i].start_date +"</td><td align='right'  style='margin:0px;padding:0px;vertical-align: middle;' id='end_date_v'>"+ data[i].end_date +"</td><td align='center'  style='margin:0px;padding:0px;vertical-align: middle;'><input disabled type='checkbox' "+state+" class='i-checks'></td><td class='edit_btn_group' style='margin:0px;padding:0px;'><button id='chglist_edit_btn' style='margin-bottom: 0px;' type='button' class='btn btn-primary btn-outline btn-block'><i class='fa fa-pencil fa-fw'></i></button></td></tr>";
                                    //$('.chg-list option[value='+data[i].list_id +']').remove();
                                });
                                $('#charglist_tbl').html(tmp);
                                //getchglist2();
                            }else{
                                $('#chglistform').show();
                            }
                            $(".chg-list").trigger("chosen:updated");
                            $('input').iCheck('update');
                        }
                    });
                }

                $('#address_close_btn').click(function(){
                    $('#address_windowe').hide();
                    $('#main_windowe').show();
                });
                $('#account_close_btn').click(function(){
                    $('#account_windowe').hide();
                    //$('#main_windowe').show();
                    $("#edit_data").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>" + sessionStorage.entity_id_e);
                });
                $('#device_close_btn').click(function(){
                    $('#account_windowe').show();
                    $('#device_windowe').hide();
                });

                $(document).on('click','#chglist_edit_btn',function(){
                    if(sessionStorage.chglistbtncountedit == '0'){
                        var listid = $(this).closest('tr').children('#list_id_v').text();
                        var listname = $(this).closest('tr').children('#list_name_v').text();
                        var startdate = $(this).closest('tr').children('#start_date_v').text();
                        var enddate = $(this).closest('tr').children('#end_date_v').text();
                        var enable_flag = $(this).closest('tr').children('#enable_flag_v').text();
                        var id = $(this).closest('tr').children('#acct_chrg_id_v').text();
                        console.log(listid);
                        $('#chglist_form_e input[name=acct_chrg_id]').val(id);
                        $('#chglist_form_e select[name=list_id]').val(listid);
                        $('#chglist_form_e input[name=s_date]').val(startdate);
                        $('#chglist_form_e input[name=e_date]').val(enddate);
                        if(enable_flag == 'Y'){
                            $('#chglist_form_e input[name=enable_flag]').attr('checked', true);
                        }else{
                            $('#chglist_form_e input[name=enable_flag]').removeAttr('checked');
                        }
                        $(this).closest('tr').children('.edit_btn_group').html("<a style='width: 50%;' id='edit_chrglist_btn' type='button' class='btn btn-primary btn-outline'><i class='fa fa-check fa-fw'></i></a><a id='undo_chglist' style='width: 50%;' type='button' class='btn btn-danger btn-outline'><i class='fa fa-undo fa-fw'></i></a>");
                        sessionStorage.chglistbtncountedit = '1';
                        $('#rmv_chglist').hide();
                        $('.chglist_xtra_btn').show();
                        $('#chglistform').show();
                        $('#chglist_save_btn').hide();
                        $(".chg-list").trigger("chosen:updated");
                        $('input').iCheck('update');
                    }
                });

                $(document).on('click','#undo_chglist',function(){
                    sessionStorage.chglistbtncountedit = '0';
                    getchglist();
                    //$(this).closest('tr').children('.edit_btn_group').html("<button id='chglist_edit_btn' type='button' class='btn btn-primary btn-outline btn-block'><i class='fa fa-pencil fa-fw'></i></button>");
                    $('.chglist_xtra_btn').hide();
                    $('#chglistform').hide();
                    $('#chglist_save_btn').show();
                });

                $(document).on('click','#save_edit_chglist',function(){
                    if($("#chglist_form_e").valid() == false){
                        notifymsg('danger','Fill Charge List');
                    }else{
                    $.ajax({
                        url: '<?php echo base_url($this->session->userdata("forajax")); ?>/upd_chrglist',
                        method: 'POST',
                        data: $('#chglist_form_e').serialize()+'&'+$.param({ 'account_id': sessionStorage.account_id_e }),
                        dataType: 'json',
                        success: function(data){
                            if(data.err == 1){
                                //notifymsg('danger', data.msg);
                                notifymsg('danger','<b>Error</b>: '+data.msg);
                            }else{
                                notifymsg('success','<b>Success</b>: Data saved.');
                                //notifytoast('success','Success', 'Change\'s Save');
                                sessionStorage.chglistbtncountedit = '0';
                                getchglist();
                                $('.chglist_xtra_btn').hide();
                                $('#chglistform').hide();
                                $('#chglist_save_btn').show();
                            }
                            $('#save_edit_chglist').removeAttr('disabled');
                        },
                        beforeSend: function(){
                            $('#save_edit_chglist').attr('disabled',true);
                        }
                    });
                    }
                });

                // function getchglist2(){
                //     $.ajax({
                //         url: '<?php echo base_url($this->session->userdata("forajax")); ?>/get_chglist2',
                //         method: 'POST',
                //         dataType: 'json',
                //         success: function(data){
                //             var tmp = '';
                //             $.each(data, function(i,item){
                //                 tmp += "<option value='"+data[i].list_id+"'>"+data[i].list_name+"</option>";
                //             });
                //             //$('#chglist_form_e select[name=list_id_2]').html(tmp);
                //             $(".chg-list2").trigger("chosen:updated");
                //         }
                //     });
                // }

                function etype_e(type){
                    if(type == "ORG"){
                        $('#entity_form_e .form-group.organization').show();
                        $('#entity_form_e .form-group.person').hide();
                    }else{
                        $('#entity_form_e .form-group.person').show();
                        $('#entity_form_e .form-group.organization').hide();
                    }
                }

                $('#entity_form_e select[name=entity_type]').change(function(){
                    if($(this).val() == "ORG"){
                        $('#entity_form_e .form-group.organization').show();
                        $('#entity_form_e .form-group.person').hide();
                    }else{
                        $('#entity_form_e .form-group.person').show();
                        $('#entity_form_e .form-group.organization').hide();
                    }
                });

                $('#account_form_edit select[name=acct_addr]').change(function(){
                    console.log($(this).val());
                    $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/get_zones'); ?>",
                        method:'post',
                        data: { 'entity_id': sessionStorage.entity_id_e, 'addr_code': $(this).val() },
                        dataType:'json',
                        success: function (data) {
                               $('#zone_form #z1').val(data[0].zone_1);$('#zone_form #z2').val(data[0].zone_2);
                               $('#zone_form #z3').val(data[0].zone_3);$('#zone_form #z4').val(data[0].zone_4);
                               $('#zone_form #z5').val(data[0].zone_5);
                               $('#account_form_edit #zone_1e').text(data[0].zone_1);
                               $('#account_form_edit #zone_2e').text(data[0].zone_2);
                        },
                        complete: function() {
                        },
                        beforeSend: function(){
                        }
                    });
                });

                $('#chglist_save_btn_e').click(function(){
                    if(sessionStorage.account_id_e == '' || sessionStorage.account_id_e == undefined){
                        notifymsg('danger','Please Add an Account First');
                    }else{
                         if($("#chglist_form_e").valid() == false){
                            notifymsg('danger','Fill Charge List');
                        }else{
                            $.ajax({
                                url: '<?php echo base_url($this->session->userdata("forajax")) . "/add_chglist" ?>',
                                method: 'POST',
                                data: $('#chglist_form_e').serialize()+'&'+$.param({ 'account_id': sessionStorage.account_id_e }),
                                dataType: 'json',
                                success: function(data){
                                    if(data.err == 1){
                                        notifymsg('danger','<b>Error</b>: '+data.msg);
                                    }else{
                                        notifymsg('success','<b>Success</b>: Data saved.');
                                    }
                                },
                                complete: function(){
                                }
                            });
                        }
                    }
                });

                $('#chglist_save_btn').click(function(){
                     if(sessionStorage.account_id_e == '' || sessionStorage.account_id_e == undefined){
                        notifymsg('danger','Please Add an Account First');
                    }else{
                        if($("#chglist_form_e").valid() == false){
                            notifymsg('danger','Fill Charge List');
                        }else{
                            $.ajax({
                            url: '<?php echo base_url($this->session->userdata("forajax")) . "/add_chglist" ?>',
                            method: 'POST',
                            data: $('#chglist_form_e').serialize()+'&'+$.param({ 'account_id': sessionStorage.account_id_e }),
                            dataType: 'json',
                            success: function(data){
                                if(data.err == 1){
                                    notifymsg('danger','<b>Error</b>: '+data.msg);
                                    notifymsg('danger',data.msg);
                                }else{
                                    notifymsg('success','<b>Success</b>: Data saved.');
                                   //$('#save_edit_address_btn').show();
                                    getchglist();
                                }
                            }
                        });
                        }
                    }
                });

                $("#chglist_form_e").validate({
                     rules: {
                         list_id: {
                            required: true
                         },
                         s_date: {
                            required: true
                         },
                         e_date: {
                            required : true
                         }
                     }
                });

                $("#entity_form_e").validate({
                     rules: {
                         entity_name: {
                            required: true
                         },
                         first_name: {
                            required : true
                         },
                         last_name: {
                            required: true
                         }
                     }
                });
                $("#entity_address_form_e").validate({
                     rules: {
                         addr_1: {
                            required: true
                         },
                         country: {
                            required: true
                         },
                         city_town: {
                            required: true
                         },
                         postal_code: {
                            required: true
                         },
                         zone_1: {
                            required: true
                         }
                     }
                });

                $("#entity_address_form_2_e").validate({
                     rules: {
                         addr_code: {
                            required: true
                         },
                         addr_1: {
                            required: true
                         },
                         country: {
                            required: true
                         },
                         city_town: {
                            required: true
                         },
                         postal_code: {
                            required: true
                         },
                         zone_1: {
                            required: true
                         }
                     }
                });

                 $("#account_form_edit").validate({
                     rules: {
                         entity_name: {
                            required: true
                         },
                         acct_type: {
                            required : true
                         },
                         acct_name: {
                            required: true
                         },
                         acctnum: {
                            required: true
                         },
                         meter_size: {
                            required: true
                         }
                     }
                });

                $('.btn.btn-sm.btn-outline.btn-info.btn-xs.edit-modal').click(function(){
                    var addr_id = $(this).closest('tr').children('#addr_id_e').text();
                    $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/getAddress'); ?>",
                        method:'post',
                        data: { 'entity_id': sessionStorage.entity_id_e, 'address_id': addr_id },
                        dataType:'json',
                        success: function (data) {
                            if(data[0].enable_flag == 'Y'){
                                $('#entity_address_form_2_e  #enable_flag_a').attr('checked', 'checked');
                            }else{
                                $('#entity_address_form_2_e  #enable_flag_a').removeAttr('checked');
                            }
                            //$('#entity_address_form_2_e #country_v').find('')
                            $('input').iCheck('update');
                            $('#entity_address_form_2_e input[name=entity_id]').val(data[0].entity_id);
                            $('#entity_address_form_2_e input[name=address_id]').val(data[0].address_id);
                            $('#entity_address_form_2_e #addr_code').val(data[0].addr_code);
                            $('#entity_address_form_2_e #addr_1').val(data[0].address_1);
                            $('#entity_address_form_2_e #addr_2').val(data[0].address_2);
                            $('#entity_address_form_2_e #addr_3').val(data[0].address_3);
                            $('#entity_address_form_2_e #citytown').val(data[0].city_town);
                            $('#entity_address_form_2_e #country_v').val(data[0].country);
                            $('#entity_address_form_2_e #purpose_v').val(data[0].purpose);
                            $('#entity_address_form_2_e #provincestate').val(data[0].state_province);
                            $('#entity_address_form_2_e #postalcode').val(data[0].postal_code);
                            $('#entity_address_form_2_e #zn_1').val(data[0].zone_1);
                            $('#entity_address_form_2_e #phone1').val(data[0].phone_1);
                            $('#entity_address_form_2_e #phone2').val(data[0].phone_2);
                            $('#entity_address_form_2_e #phone3').val(data[0].phone_3);
                            $('#address_windowe').show();
                            $('#main_windowe').hide();
                            $('#save_edit_address_btn').show();
                            $('#save_add_address_btn').hide();
                            $("select").trigger("chosen:updated");
                            getzoness(data[0].zone_1,data[0].zone_2,data[0].zone_3,data[0].zone_4,data[0].zone_5);
                        },
                        complete: function() {
                            $('#loading-icon-addr').hide();
                        },
                        beforeSend: function(){
                            $('#loading-icon-addr').show();
                        }
                    });
                });


                $('.btn.btn-sm.btn-outline.btn-info.btn-xs.edit-modal-acc').click(function(){
                    sessionStorage.chglistbtncountedit = '0';
                    $('#save_add_acc_btn').hide();
                    $('#save_edit_acc_btn').show();
                    var acct_id = $(this).closest('tr').children('#acct_id').text();
                    var acct_addr = $(this).closest('tr').children('#acct_addr').text();
                    sessionStorage.account_id_e = acct_id;
                    getchglist();
                    $('#chglist_form_e')[0].reset();
                    $('select').trigger('chosen:updated');
                    $('#chglistform').show();
                    $('.chglist_xtra_btn').hide();
                    $('#chglistform').hide();
                    $('#chglist_save_btn').show();
                    $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/getAccount'); ?>",
                        method:'post',
                        data: { 'entity_id': sessionStorage.entity_id_e, 'account_id': acct_id, 'acct_addr': acct_addr },
                        dataType:'json',
                        success: function (data) {
                            if(data[0].enable_flag == 'Y'){
                                $('#account_form_edit #ena_flag_v').attr('checked','checked');
                            }else{
                                $('#account_form_edit #ena_flag_v').removeAttr('checked');
                            }
                            $('input').iCheck('update');
                            $('#account_form_edit input[name=entity_id]').val(data[0].entity_id);
                            $('#account_form_edit input[name=account_id]').val(data[0].account_id);
                            $('#account_form_edit #entityname').val(data[0].entity_name);
                            $('#account_form_edit #acctaddr').val(data[0].acct_addr);
                            $('#account_form_edit #accttype').val(data[0].acct_type);
                            $('#account_form_edit #mtrsize').val(data[0].meter_size);
                            $('#account_form_edit #status_v').val(data[0].status);
                            $('#account_form_edit #status_v2').val(data[0].status_meaning);
                            $('#account_form_edit #acctname').val(data[0].acct_name);
                            $('#account_form_edit #acctnum').val(data[0].acct_num);
                            $('#account_form_edit #meterseq').val(data[0].meter_seq);
                            $('#account_form_edit #billtoaddr').val(data[0].bill_to_addr);
                            $('#account_form_edit #activestart').val(data[0].active_start_f);
                            $('#account_form_edit #activeend').val(data[0].active_end_f);
                            $('#account_form_edit #lat_ea').val(data[0].latitude);
                            $('#account_form_edit #lng_ea').val(data[0].longitude);
                            $('#account_form_edit #attentionto').val(data[0].attention_to);
                            $('#account_form_edit #z1').val(data[0].zone_1);
                            $('#account_form_edit #z2').val(data[0].zone_2);
                            $('#account_form_edit #z3').val(data[0].zone_3);
                            $('#account_form_edit #z4').val(data[0].zone_4);
                            $('#account_form_edit #z5').val(data[0].zone_5);
                            $('#account_form_edit #account_typee').text(data[0].acct_type);
                            $('#account_form_edit #meter_sizee').text(data[0].meter_size);
                            $('#account_windowe').show();
                            $('#main_windowe').hide();
                            $("select").trigger("chosen:updated");
                            zonedaw();
                            initialize();
                        },
                        complete: function() {
                        },
                        beforeSend: function(){
                        }
                    });

                     $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/get_device'); ?>",
                        method:'post',
                        data: {'account_id': acct_id},
                        dataType:'json',
                        success: function (data) {
                            var tmp = ''
                            var count = 1;
                            $.each(data, function(i, item) {
                                 tmp += "<tr><td>"+count+"</td><td style='display:none;' id='dev_id_edit'>"+data[i].device_assignment_id+"</td><td>"+data[i].device_id+"</td><td>"+data[i].model_number+"</td><td>"+data[i].serial_number+"</td><td>"+data[i].dev_stat_meaning+"</td><td><button class='btn btn-sm btn-outline btn-info btn-xs edit-dev-modal'><i class='fa fa-pencil-square-o fa-lg'></i></button></td><tr>";
                                 count++;
                            });
                            $('#device_table_edit').html(tmp);
                        },
                        complete: function() {
                        },
                        beforeSend: function(){
                        }
                    });
                });

                function zonedaw(){
                    $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/get_zones'); ?>",
                        method:'post',
                        data: { 'entity_id': sessionStorage.entity_id_e, 'addr_code': $('#account_form_edit select[name=acct_addr]').val() },
                        dataType:'json',
                        success: function (data) {
                               $('#zone_form #z1').val(data[0].zone_1);$('#zone_form #z2').val(data[0].zone_2);
                               $('#zone_form #z3').val(data[0].zone_3);$('#zone_form #z4').val(data[0].zone_4);
                               $('#zone_form #z5').val(data[0].zone_5);
                               $('#account_form_edit #zone_1e').text(data[0].zone_1);
                               $('#account_form_edit #zone_2e').text(data[0].zone_2);

                        }
                    });
                }

                $('#add_addr_btn_e').click(function(){
                    $('#save_edit_address_btn').hide();
                    $('#save_add_address_btn').show();
                    $("#entity_address_form_2_e")[0].reset();
                    $('select').trigger('chosen:updated');
                    $('#address_windowe').show();
                    $('#main_windowe').hide();
                });

                $('#save_add_address_btn').click(function(){
                    if($("#entity_address_form_2_e").valid() == true){
                        inserAddrE($("#entity_address_form_2_e").serialize(),sessionStorage.entity_id_e);
                    }
                });

                $('#save_edit_entity').click(function(){
                    if($("#entity_form_e").valid() == true && $("#entity_address_form_e").valid() == true){
                        $.ajax({
                            url: "<?php echo base_url($this->session->userdata('forajax') . '/update'); ?>",
                            method:'post',
                            data:$('#entity_form_e').serialize(),
                            dataType:'json',
                            success: function (data) {
                                if(data.err == 1){
                                   notifymsg('danger','<b>Error</b>: '+data.msg);
                                }else{
                                    notifymsg('success','<b>Success</b>: Data saved.');
                                    //sessionStorage.entity_id = data.id;
                                    $("#entity_form").find(':input:not(:disabled)').prop('disabled',true);
                                    $('#save_edit_entity').removeAttr('id');
                                    updateAddr($("#entity_address_form_e").serialize());
                                }
                            },
                            complete: function() {
                                $('#save_edit_entity').removeAttr("disabled");
                                $('#loading-icon').hide();
                            },
                            beforeSend: function(){
                                $('#save_edit_entity').attr("disabled", true);
                                $('#loading-icon').show();
                            }
                        });
                    }
                });

                $('#add_acc_btn_edit').click(function(){
                    getMeterSeq2();
                    getflexseqs2_e()
                    $('#device_table_edit').html('');
                    sessionStorage.account_id_e = '';
                    $('select').trigger('chosen:updated');
                    $('#account_windowe').show();
                    $('#main_windowe').hide();
                    $('#save_add_acc_btn').show();
                    $('#save_edit_acc_btn').hide();
                    $('#account_form_edit #ena_flag_v').attr('checked','checked');
                    $('.i-checks').iCheck('update');
                    $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/get_account_num'); ?>",
                        method:'post',
                        dataType:'json',
                        success: function (data) {
                            $('#account_form_edit #acctnum').val(data[0].val);
                        }
                    });
                    getCode();
                    initialize_add();
                });

                $('#save_edit_address_btn').click(function(){
                    if($("#entity_address_form_2_e").valid() == true){
                        updateAddr($("#entity_address_form_2_e").serialize());
                    }
                });

                $('#account_edit').on('hide.bs.modal', function () {
                    //$("#edit_data").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>" + sessionStorage.entity_id_e);
                });

                $('#save_add_acc_btn').click(function(){
                    if($("#account_form_edit").valid() == true){
                        //var acct_num = $('#account_form_edit #zone_1e').text() + $('#account_form_edit #zone_2e').text() + '-' + $('#account_form_edit #account_typee').text() + $('#account_form_edit #meter_sizee').text() + '-' + $('#account_form_edit #acctnum').val();
                        var acct_num = concat_flexee();
                        $.ajax({
                            url: "<?php echo base_url($this->session->userdata('forajax') . '/insert_acc'); ?>",
                            method:'post',
                            data: $('#account_form_edit').serialize()+'&'+$.param({ 'entity_id': sessionStorage.entity_id_e, 'acct_num': acct_num }),
                            dataType:'json',
                            success: function (data) {
                                if(data.err == 1){
                                   notifymsg('danger','<b>Error</b>: '+data.msg);
                                   $('#save_add_acc_btn').removeAttr('disabled');
                                }else{
                                    notifymsg('success','<b>Success</b>: Data saved.');
                                    sessionStorage.account_edit_changes = 1;
                                    sessionStorage.account_id_e = data.id;
                                    create_work_order(data.id);
                                    $('#suc-err-add').html('<div class="minimalize-styl-2 alert alert-success alert-dismissable" style="padding-right: 30px;"><button style="margin-left: -5px;" aria-hidden="true" data-dismiss="alert" class="close" type="button">x</button><b>Success:</b> Data Saved</div>');
                                    //$('#account_form_edit input[name=acctnum]').val(data.accnum);
                                    $('#save_add_acc_btn').attr('disabled','disabled');
                                }
                            },
                            complete: function() {
                                $('#loading-icon-acc').hide();
                            },
                            beforeSend: function(){
                                $('#loading-icon-acc').show();
                                $('#save_add_acc_btn').attr('disabled','disabled');
                            }
                        });
                    }
                });

                $('#save_edit_acc_btn').click(function(){
                    if($("#account_form_edit").valid() == true){
                        //var acct_num = $('#account_form_edit #zone_1e').text() + $('#account_form_edit #zone_2e').text() + '-' + $('#account_form_edit #account_typee').text() + $('#account_form_edit #meter_sizee').text() + '-' + $('#account_form_edit #acctnum').val();
                        var acct_num = $('#account_form_edit #acctnum').val();
                        $.ajax({
                            url: "<?php echo base_url($this->session->userdata('forajax') . '/update_acc'); ?>",
                            method:'post',
                            data: $('#account_form_edit').serialize()+'&'+$.param({ 'entity_id': sessionStorage.entity_id_e, 'acct_num': acct_num }),
                            dataType:'json',
                            success: function (data) {
                                if(data.err == 1){
                                   notifymsg('danger','<b>Error</b>: '+data.msg);
                                }else{
                                    sessionStorage.account_edit_changes = 1;
                                    notifymsg('success','<b>Success</b>: Data saved.');
                                    //$('#account').modal('hide');
                                    $('#suc-err-add').html('<div class="minimalize-styl-2 alert alert-success alert-dismissable" style="padding-right: 30px;"><button style="margin-left: -5px;" aria-hidden="true" data-dismiss="alert" class="close" type="button">x</button><b>Success:</b> Data Saved</div>');
                                    //sessionStorage.account_id = data.id;
                                    $('#save_account_btn').prop('disabled',true);
                                }
                            },
                            complete: function() {
                                $('#loading-icon-acc').hide();
                            },
                            beforeSend: function(){
                                $('#loading-icon-acc').show();
                            }
                        });
                    }
                });

                function getCode(){
                     $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/get_ena_addr'); ?>",
                        method:'post',
                        data: { 'entity_id': sessionStorage.entity_id_e },
                        dataType:'json',
                        success: function (data) {
                            console.log(data);
                            var tmp = '';
                            var tae = 1;
                            var igit = '';
                            $.each(data, function(i,items){
                                igit = '';
                                if(tae == 1){
                                    igit = 'selected';
                                    tae++;
                                }
                                $('#account_form_edit input[name=entity_name]').val(data[i].entity_name);
                                $('#account_form_edit input[name=acct_name]').val(data[i].entity_name);
                               tmp += "<option value='"+data[i].address_id+"' "+igit+">"+data[i].addr_code+"</option>";
                            });
                            $('#account_form_edit select[name=acct_addr]').html(tmp);
                            $('#account_form_edit select[name=bill_to_addr]').html(tmp);
                            zonedaw();
                        },
                        complete: function() {
                        },
                        beforeSend: function(){
                        }
                    });
                }

                function updateAddr(forms) {
                    $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/update_addr'); ?>",
                        method:'post',
                        data: forms,
                        dataType:'json',
                        success: function (data) {
                            if(data.err == 1){
                                notifymsg('danger','<b>Error</b>: '+data.msg);
                            }else{
                                sessionStorage.address_edit_changes = 1;
                                $('#address_windowe').hide();
                                //$('#main_windowe').show();
                                notifymsg('success','<b>Success</b>: Data saved.');
                                $("#edit_data").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>" + sessionStorage.entity_id_e);
                                //getAddress(sessionStorage.entity_id_e);
                            }
                        },
                        complete: function() {
                            $('#loading-icon-addr-edit').hide();
                            $("#save_edit_address_btn").removeAttr("disabled");
                        },
                        beforeSend: function(){
                            $('#loading-icon-addr-edit').show();
                            $("#save_edit_address_btn").attr("disabled", true);
                        }
                    });
                }

                function inserAddrE(forms,id) {
                    $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/insert_addr'); ?>",
                        method:'post',
                        data: forms+'&'+$.param({ 'entity_id': id }),
                        dataType:'json',
                        success: function (data) {
                            if(data.err == 1){
                                notifymsg('danger','<b>Error</b>: '+data.msg);
                            }else{
                                sessionStorage.address_edit_changes = 1;
                                notifymsg('success','<b>Success</b>: Data saved.');
                                $('#address_windowe').hide();
                                //$('#main_windowe').show();
                                $("#edit_data").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>" + sessionStorage.entity_id_e);
                                //getAddress(sessionStorage.entity_id_e);
                            }
                        },
                        complete: function() {
                            $('#loading-icon-addr').hide();
                            //$("#save_entity").removeAttr("disabled");
                        },
                        beforeSend: function(){
                            $('#loading-icon-addr').show();
                            //$("#save_entity").attr("disabled", true);
                        }
                    });
                }

                function getAddress(id){
                    $('#table_address').html('');
                    $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/getAddress2'); ?>",
                        method:'post',
                        data: { 'entity_id': id },
                        dataType:'json',
                        success: function (data) {
                            var tmp = '';
                            var count = 1;
                            var enable = "";
                            $.each(data, function(i, item) {
                                if(data[i].enable_flag == 'Y'){
                                    enable = "checked";
                                }
                                if(data[i].addr_code !== 'MAIN'){
                                    tmp += "<tr><td>"+count+"</td><td>"+data[i].address_1+"</td><td>"+data[i].addr_code+"</td><td align='center'><div class='state icheckbox_square-green "+ enable +"'></div></td></tr>"
                                    count++;
                                }
                            });
                            $('#table_address').html(tmp);
                        }
                    });
                }

                $('#add_dev_btn_edit').click(function(){
                    if(typeof sessionStorage.account_id_e == 'undefined' || sessionStorage.account_id_e == ''){
                       notifymsg('danger', 'Add an Account First.')
                    }else{
                        console.log(sessionStorage.account_id_e);
                        $('#device_windowe').show();
                        $('#account_windowe').hide();
                        $('#device_windowe form')[0].reset();
                        $('select').trigger('chosen:updated');
                        $('#ins_dev_btn_edit').show();
                        $('#upd_dev_btn').hide();
                        $('#device_form_e input[name=account_id]').val(sessionStorage.account_id_e);
                        $('#ins_dev_btn_edit').removeAttr('disabled');
                    }
                });

               $(document).on('click','.edit-dev-modal',function(){
                    $('#ins_dev_btn_edit').hide();
                    $('#upd_dev_btn').show();
                    $('#device_windowe').show();
                    $('#account_windowe').hide();
                    var dev_id = $(this).closest('tr').children('#dev_id_edit').text();
                    $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/get_device2'); ?>",
                        method:'post',
                        data: {'device_assignment_id': dev_id },
                        dataType:'json',
                        success: function (data) {
                            $('#device_form_e input[name=device_ass_id]').val(data[0].device_assignment_id);
                            $('#device_form_e input[name=account_id]').val(data[0].account_id);
                            $('#device_form_e input[name=dev_id]').val(data[0].device_id);
                            $('#device_form_e input[name=e_lat_d]').val(data[0].loc_latitude);
                            $('#device_form_e input[name=e_lng_d]').val(data[0].loc_longitude);
                            $('#device_form_e select[name=item_num]').val(data[0].item_id);
                            $('#device_form_e select[name=dev_type]').val(data[0].device_type);
                            $('#device_form_e input[name=model_number]').val(data[0].model_number);
                            $('#device_form_e input[name=serial_number]').val(data[0].serial_number);
                            $('#device_form_e input[name=install_date]').val(data[0].install_date);
                            $('#device_form_e input[name=warranty_per]').val(data[0].warranty_period);
                            $('#device_form_e input[name=initial_read]').val(data[0].initial_reading);
                            $('#device_form_e input[name=last_read]').val(data[0].last_reading);
                            $('#device_form_e input[name=maint_cycle]').val(data[0].maintenance_cycle);
                            $('#device_form_e input[name=repl_cycle]').val(data[0].replacement_cycle);
                            $('#device_form_e select[name=maint_cycle_unit]').val(data[0].maintenance_cycle_unit);
                            $('#device_form_e select[name=repl_cycle_unit]').val(data[0].replacement_cycle_unit);
                            $('#device_form_e select[name=status]').val(data[0].status);
                            $('#device_form_e input[name=dev_remarks]').val(data[0].device_remarks);
                            $("select").trigger("chosen:updated");
                        },
                        complete: function() {
                            $('#loading_dev_edit').hide();
                            $('#device_form_e').show();
                            initialize_dev();
                        },
                        beforeSend: function(){
                            $('#device_form_e').hide();
                            $('#loading_dev_edit').show();
                        }
                    });
                });

                $("#device_form_e").validate({
                     rules: {
                         item_num: {
                            required: true
                         },
                         dev_type: {
                            required: true
                         },
                         model_number: {
                            required: true
                         },
                         serial_number: {
                            required: true
                         }
                     }
                });

                $('#ins_dev_btn_edit').click(function(){
                    var err = 0;
                    if( $("#device_form_e").valid() == true){
                        $('#suc-err-dev-edit').hide();
                        $.ajax({
                            url: "<?php echo base_url($this->session->userdata('forajax') . '/insert_dev'); ?>",
                            method:'post',
                            data: $('#device_form_e').serialize(),
                            dataType:'json',
                            success: function (data) {
                                err = data.err;
                                if(data.err == 1){
                                   $('#ins_dev_btn_edit').removeAttr('disabled');
                                   notifymsg('danger','<b>Error</b>: '+data.msg);
                                }else{
                                    $('#device_windowe').hide();
                                    $('#account_windowe').show();
                                    notifymsg('success','<b>Success</b>: Data saved.');
                                    getDeviceEdit();
                                    $('#ins_dev_btn_edit').attr('disabled','disabled');
                                }
                            },
                            complete: function() {
                                $('#loading-icon-dev-edit').hide();
                            },
                            beforeSend: function(){
                                $('#loading-icon-dev-edit').show();
                                $('#ins_dev_btn_edit').attr('disabled',true);
                            }
                        });
                    }
                });

                function getDeviceEdit(){
                    $.ajax({
                                url: "<?php echo base_url($this->session->userdata('forajax') . '/get_device'); ?>",
                                method:'post',
                                data: {'account_id': sessionStorage.account_id_e},
                                dataType:'json',
                                success: function (data) {
                                    var tmp = ''
                                    var count = 1;
                                    $.each(data, function(i, item) {
                                         tmp += "<tr><td>"+count+"</td><td style='display:none;' id='dev_id_edit'>"+data[i].device_assignment_id+"</td><td>"+data[i].device_id+"</td><td>"+data[i].model_number+"</td><td>"+data[i].serial_number+"</td><td>"+data[i].dev_stat_meaning+"</td><td><button class='btn btn-sm btn-outline btn-info btn-xs edit-dev-modal'><i class='fa fa-pencil-square-o fa-lg'></i></button></td><tr>";
                                         count++;
                                    });
                                    $('#device_table_edit').html(tmp);
                                },
                                complete: function() {
                                },
                                beforeSend: function(){
                                }
                            });
                }

                $('#upd_dev_btn').click(function(e) {
                    if($("#device_form_e").valid() == true){
                        $.ajax({
                            url: "<?php echo base_url($this->session->userdata('forajax') . '/update_dev'); ?>",
                            method:'post',
                            data: $('#device_form_e').serialize(),
                            dataType:'json',
                            success: function (data) {
                                if(data.err == 1){
                                    notifymsg('danger','<b>Error</b>: '+data.msg);
                                }else{
                                    notifymsg('success','<b>Success</b>: Data saved.');
                                    $('#device_windowe').hide();
                                    $('#account_windowe').show();
                                    getDeviceEdit();
                                    //$("#edit_data").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>" + sessionStorage.entity_id_e);
                                }
                            },
                            complete: function() {
                                $('#loading-icon-dev-edit').hide();
                                $("#upd_dev_btn").removeAttr("disabled");
                            },
                            beforeSend: function(){
                                $('#loading-icon-dev-edit').show();
                                $("#upd_dev_btn").attr("disabled", true);
                            }
                        });
                    }
                });

                $('#account_edit').on('hide.bs.modal', function () {
                    if(sessionStorage.account_edit_changes == 1){
                        $("#edit_data").html('<div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div>');
                        $("#edit_data").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>" + sessionStorage.entity_id_e);
                        sessionStorage.account_edit_changes = 0;
                    }
                });

                $('#addresses_e').on('hide.bs.modal', function () {
                    if(sessionStorage.address_edit_changes == 1){
                        $("#edit_data").html('<div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div>');
                        $("#edit_data").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>" + sessionStorage.entity_id_e);
                        sessionStorage.address_edit_changes = 0;
                    }
                });

                $(document).on({
                    'show.bs.modal': function () {
                        var zIndex = 1040 + (10 * $('.modal:visible').length);
                        $(this).css('z-index', zIndex);
                        setTimeout(function() {
                            $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
                        }, 0);
                    },
                    'hidden.bs.modal': function() {
                        if ($('.modal:visible').length > 0) {
                            // restore the modal-open class to the body element, so that scrolling works
                            // properly after de-stacking a modal.
                            setTimeout(function() {
                                $(document.body).addClass('modal-open');
                            }, 0);
                        }
                    }
                }, '.modal');
        // $('#entity_form_e').data('serialize',$('#entity_form_e').serialize());
        function getMeterSeq2(){
                   $.ajax({
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/getMeterSeq'); ?>",
                        method:'get',
                        dataType:'json',
                        success: function (data) {
                            $.each(data,function(i,item) {
                                 $('#account_form_edit input[name=meter_seq]').val(data[i].meter_seq);
                            });    
                        }
                    });
                }
        function alert_cancel(){
                    if($('#entity_form_e').serialize()!=$('#entity_form_e').data('serialize')){
                        swal({
                              title: "Are you sure?",
                              text: "Your work will not be save",
                              type: "warning",
                              showCancelButton: true,
                              confirmButtonColor: "#DD6B55",
                              confirmButtonText: "Leave",
                              cancelButtonText: "Stay",
                              closeOnConfirm: true,
                              closeOnCancel: true },
                          function (isConfirm) {
                              if (isConfirm) {
                                  close_edit(); active_search();
                                  notifymsg("hide","hide");
                              } else {
                                  swal("Cancelled");
                              }
                          });
                        }else{
                        close_edit(); active_search();
                        notifymsg("hide","hide");
                    }
                }
                function  create_work_order(cus_id){
                    var someDate = new Date();
                    var numberOfDaysToAdd = 6;
                    someDate.setDate(someDate.getDate() + numberOfDaysToAdd); 

                    var dd = someDate.getDate();
                    var mm = someDate.getMonth() + 1;
                    var y = someDate.getFullYear();

                    var someFormattedDate = y + '-'+ mm + '-'+ dd;
                    //console.log(someFormattedDate);
                    $.ajax({
                            url: "<?php echo base_url($this->session->userdata('forajax') . '/create_wo'); ?>",
                            method:'post',
                            data: {"acct_id":cus_id, "target_date" : someFormattedDate},
                            dataType:'json',
                            success: function (data) {
                                if(data.err == 1){
                                   notifymsg('danger','<b>Error</b>: '+data.msg);
                                   
                                }else{
                                    
                                }
                            },
                            complete: function() {
                            
                            },
                            beforeSend: function(){
                       
                            }
                        });

                    
                }
                $('#cancel_edit').click(function(){
                    alert_cancel();
                });
              $('#editX').click(function(){
                    alert_cancel();
              });
              $("select").trigger("chosen:updated");
            });
    </script>